package com.yanqu.road.dao.impl.activity.jiuzhou;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.jiuzhou.rank.JiuZhouRank;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JiuZhouRankDaoImpl extends TempDataObjectDao<JiuZhouRank> {
    public static String getJiuZhouRankSql = "select * from `t_c_jiu_zhou_rank` where `activityId`=? and `type`=? and `zoneType`=? and `userId`=?";
    public JiuZhouRank getJiuZhouRank(int activityId,int type,int zoneType,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getJiuZhouRankSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, type);
            call.setInt(i++, zoneType);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String getJiuZhouRankMapSql = "select * from `t_c_jiu_zhou_rank` where `activityId`=?";
    public List<JiuZhouRank> getJiuZhouRank(int activityId) {
        List<JiuZhouRank> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getJiuZhouRankMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateJiuZhouRankSql = "update `t_c_jiu_zhou_rank` set lastUpdateTime=?,value=?,param=? where activityId=? and type=? and zoneType=? and userId=?";
    @Override
    protected boolean updateData(JiuZhouRank jiuZhouRank) {
        jiuZhouRank.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, jiuZhouRank.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, jiuZhouRank.getValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jiuZhouRank.getParam())));
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouRank.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouRank.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouRank.getZoneType()));
        para.put(i++, new DbParameter(Types.BIGINT, jiuZhouRank.getUserId()));
        if (getDb().executeNoneQuery(updateJiuZhouRankSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertJiuZhouRankSql = "insert into `t_c_jiu_zhou_rank`(activityId,type,zoneType,userId,lastUpdateTime,value,param) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(JiuZhouRank jiuZhouRank) {
        jiuZhouRank.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouRank.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouRank.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, jiuZhouRank.getZoneType()));
        para.put(i++, new DbParameter(Types.BIGINT, jiuZhouRank.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, jiuZhouRank.getLastUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, jiuZhouRank.getValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(jiuZhouRank.getParam())));
        if (getDb().executeNoneQuery(insertJiuZhouRankSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected JiuZhouRank parseData(ResultSet rs) throws SQLException {
        JiuZhouRank jiuZhouRank = new JiuZhouRank();
        jiuZhouRank.setActivityId(rs.getInt("activityId"));
        jiuZhouRank.setType(rs.getInt("type"));
        jiuZhouRank.setZoneType(rs.getInt("zoneType"));
        jiuZhouRank.setUserId(rs.getLong("userId"));
        jiuZhouRank.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        jiuZhouRank.setValue(rs.getLong("value"));
        String param = rs.getString("param");
        if (!StringUtils.isNullOrEmpty(param)) {
            jiuZhouRank.setParam(JSON.parseObject(param, new TypeReference<List<Long>>() {
            }));
        }

        return jiuZhouRank;
    }

}
